package com.wanmait.deliverymangementsystem.usercontroller;

import com.wanmait.deliverymangementsystem.pojo.City;
import com.wanmait.deliverymangementsystem.pojo.Province;
import com.wanmait.deliverymangementsystem.pojo.UserAddress;
import com.wanmait.deliverymangementsystem.pojo.Users;
import com.wanmait.deliverymangementsystem.service.CityService;
import com.wanmait.deliverymangementsystem.service.ProvinceService;
import com.wanmait.deliverymangementsystem.service.UserAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserAddressController {
    @Resource
    private CityService cityService;
    @Resource
    private ProvinceService provinceService;
    @Resource
    private UserAddressService userAddressService;


    //添加地址
    @GetMapping("addAddress")
    @ModelAttribute("provinceList")
    public List<Province> addAddress(HttpServletRequest request){
        List<Province> provinceList =provinceService.findAll();
        List<City> beijingList=cityService.findCityByProvinceID(1);
        request.setAttribute("beijingList",beijingList);
     return  provinceList;
    }
    @GetMapping("searchCity")
    @ResponseBody
    public List<City> searchCity(Integer provinceID){
        List<City>  cityList =  cityService.findCityByProvinceID(provinceID);
      return cityList;
    }
    @PostMapping("addAddress")
    public String addAddress(UserAddress address, HttpSession session){
        address.setUser((Users) session.getAttribute("user"));
        userAddressService.addAddress(address);
        return "redirect:/user/myAddress";

    }

//修改地址
    @GetMapping("updateAddress")
    public UserAddress updateAddress(Integer id, Model model){
        UserAddress  address= userAddressService.findAddress(id);
        model.addAttribute("address",address);
        List<Province> provinceList= provinceService.findAll();
        model.addAttribute("provinceList",provinceList);
        List<City> beijingList=cityService.findCityByProvinceID(1);
        model.addAttribute("beijingList",beijingList);
        return address;
    }
    @PostMapping("updateAddress")
    public String updateAddress(UserAddress userAddress)
    {
        userAddressService.updateMyAddress(userAddress);
        return "redirect:/user/myAddress";
    }
        //查看地址

    @GetMapping("myAddress")
    @ModelAttribute("addressList")
    public List<UserAddress> myAddress(HttpSession session){
        List<UserAddress> addressList =userAddressService.showUserAddress((Users)session.getAttribute("user"));
        for (UserAddress address:addressList
        ) {
            System.out.println(address+":"+address.getCity());
            address.setCity(cityService.findCityByID(address.getCity().getId()));
        }
        return addressList;
    }

    //删除地址
    @GetMapping("deletMyAddress")
    public String deletMyAddress(Integer id){
        userAddressService.delete(id);
        return "redirect:/user/myAddress";
    }
}
